<?php

namespace app\admin\controller\god;

use app\common\controller\Backend;

class Major extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Major');
    }

    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $result = ['total' => 0, 'rows' => []];
            $total = $this->model->count();
            if (intval($total) > 0) {
                $result['total'] = $total;
                $list = $this->model->select();
                list($cgTypes, $cTypes, $mcTypes, $lwTypes, $elTypes, $eaTypes) = $this->getEleTypes(true);
                foreach ($list as $k => $row) {
                    $row['read_price_text'] = bcdiv(intval($row['read_price']), 100, 2);

                    $row['pro_grade_text'] = '';
                    if (isset($cgTypes[$row['pro_grade']])) $row['pro_grade_text'] = $cgTypes[$row['pro_grade']];
                    $row['college_name'] = '';
                    if (isset($cTypes[$row['college_id']])) $row['college_name'] = $cTypes[$row['college_id']];
                    $row['pro_level_text'] = '';
                    if (isset($mcTypes[$row['pro_grade']])) $row['pro_level_text'] = $mcTypes[$row['pro_level']];
                    $row['lessons_way_text'] = '';
                    if (isset($lwTypes[$row['lessons_way']])) $row['lessons_way_text'] = $lwTypes[$row['lessons_way']];
                    $row['exam_lessons_text'] = '';
                    if (isset($elTypes[$row['exam_lessons']])) $row['exam_lessons_text'] = $elTypes[$row['exam_lessons']];
                    $row['exam_area_text'] = '';
                    if (isset($eaTypes[$row['exam_area_id']])) $row['exam_area_text'] = $eaTypes[$row['exam_area_id']];
                }
                $result['rows'] = $list;
            }

            return json($result);
        }
        return $this->view->fetch();
    }


    private function getEleTypes($isDataMap = false)
    {
        // 年级
        $cgTypes = model('Collegegrade')->column('name', 'id');
        // 院校
        $cTypes = model('College')->column('co_name', 'id');
        // 专业层次
        $mcTypes = model('Stureadlevelinfo')->column('name', 'id');
        // 授课方式
        $lwTypes = model('Lessonsway')->column('name', 'id');
        // 考区
        $exam_area = model('Examarea')->column('name', 'id');
        // 考试科目
        $elTypes = model('Examlessons')->column('name', 'id');
        if ($isDataMap) {
            return [$cgTypes, $cTypes, $mcTypes, $lwTypes, $elTypes, $exam_area];
        }
        $this->view->assign('college_grade_types', $cgTypes);
        $this->view->assign('college_types', $cTypes);
        $this->view->assign('ml_types', $mcTypes);
        $this->view->assign('lw_types', $lwTypes);
        $this->view->assign('exam_area', $exam_area);
        $this->view->assign('el_types', $elTypes);
        return [];
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args['name']) || empty($args['pro_code'])) {
                $this->error('专业名称与编码都不能为空');
            }
            $ck = $this->model->where('pro_code', $args['pro_code'])->find();
            if (!empty($ck)) {
                $this->error('专业编码已存在，不能重复添加');
            }
            if (!empty($args['read_price'])) {
                $args['read_price'] = floatval($args['read_price']) * 100; // 单位为分
            } else {
                $args['read_price'] = 0;
            }
            $data = $this->getMajorByArgs($args);
            $save = $this->model->data($data)->save();
            if (empty($save)) {
                $this->error('保存失败');
            }
            $this->success('操作成功');
        }
        $this->getEleTypes();
        return $this->view->fetch();
    }


    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (empty($row)) {
            $this->error('参数异常');
        }
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args)) {
                $this->error('参数错误');
            }
            if (!empty($args['read_price'])) {
                $args['read_price'] = floatval($args['read_price']) * 100; // 单位为分
            } else {
                $args['read_price'] = 0;
            }
            $data = $this->getMajorByArgs($args);
            $save = $this->model->save($data, ['id' => intval($ids)]);
            if (empty($save)) {
                $this->error('更新失败');
            }
            $this->success('更新成功');
        }
        $this->getEleTypes();
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    private function getMajorByArgs($args): array
    {
        return [
            'college_id' => $args['college_id'],
            'name' => $args['name'],
            'pro_code' => $args['pro_code'],
            'read_price' => $args['read_price'], // 价格单位为分
            'pro_year' => $args['pro_year'],
            'pro_grade' => $args['pro_grade'],
            'pro_level' => $args['pro_level'],
            'pro_category' => $args['pro_category'],
            'lessons_way' => $args['lessons_way'],
            'is_edu_class' => $args['is_edu_class'],
            'exam_area_id' => $args['exam_area_id'],
            'exam_lessons' => $args['exam_lessons'],
            'remark' => $args['remark'],
            'status' => $args['status']
        ];
    }
}